// miniprogram/pages/activity_detail/activity_detail.js
var func = require('../../utils/util.js')
var global = getApp().globalData;
var db = wx.cloud.database();
var activityId;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    join: false,
    collect: false
  },

  onCollect: function () {
    if (this.data.collect) {
      wx.showModal({
        cancelColor: 'cancelColor',
        title: '确定取消收藏吗',
        success: res => {
          if (res.confirm) {
            wx.showLoading({
              title: '取消中',
            })
            db.collection('collect').where({
              _openid: global.openid,
              activityId: activityId
            }).remove().then(res => {
              func.hideLoading('取消收藏成功')
              this.setData({
                collect: false
              })
            }).catch(res => {
              func.hideLoading('取消失败')
            })
          }
        }
      })
    } else {
      db.collection('collect').add({
        data: {
          time: func.formatTime(),
          activityId: activityId
        }
      }).then(res => {
        func.hideLoading('收藏成功')
        this.setData({
          collect: true
        })
      }).catch(res => {
        func.hideLoading('收藏失败')
      })
    }
  },

  cancelJoin: function () {
    wx.showModal({
      cancelColor: 'cancelColor',
      title: '确定取消报名吗',
      success: res => {
        if (res.confirm) {
          wx.showLoading({
            title: '取消中',
          })
          const _ = db.command;
          db.collection('activityInfo').doc(activityId).get().then(res => {
            var info = res.data.joinInfo;
            const len = info.length;
            for (let i = 0; i < len; i++) {
              if (info[i].openid == global.openid) {
                info.splice(i, 1)
              }
            }
            db.collection('activityInfo').doc(activityId).update({
              data: {
                joinInfo: info
              }
            }).then(res => {
              db.collection('myJoin').where({
                _openid: global.openid,
                activityId: activityId
              }).remove().then(res => {
                func.hideLoading('取消成功')
                this.setData({
                  join: false
                })
              })
            }).catch(res => {
              func.hideLoading('取消失败')
            })
          }).catch(res => {
            func.hideLoading('取消失败')
          })
        }
      }
    })
  },

  onJoin: function () {
    if (this.data.timeOut) {
      wx.showLoading({
        title: '报名中',
      })
      var myInfo = {
        openid: global.openid,
        name: global.name,
        sex: global.sex,
        schoolId: global.schoolId,
        myClass: global.myClass,
        department: global.department,
        time: func.formatTime()
      }
      const _ = db.command;
      db.collection('activityInfo').doc(activityId).update({
        data: {
          joinInfo: _.addToSet(myInfo)
        }
      }).then(res => {
        db.collection('myJoin').add({
          data: {
            activityId: activityId,
          }
        }).then(res => {
          func.hideLoading('报名成功')
          this.setData({
            join: true
          })
        }).catch(res => {
          func.hideLoading('报名失败')
        })
      }).catch(res => {
        func.hideLoading('报名失败')
      })
    } else {
      wx.showToast({
        title: '报名时间已过',
        icon: "none"
      })
    }
  },

  retJion: function (arr) {
    for (let a = 0; a < arr.length; a++) {
      if (arr[a].openid == global.openid) {
        this.setData({
          join: true
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    activityId = options.id;
    wx.showLoading({
      title: '加载中',
    })
    db.collection('activityInfo').doc(activityId).get().then(res => {
      var time = new Date(res.data.info1.particulars[1].content).getTime()
      this.setData({
        info1: res.data.info1,
        info2: res.data.info2,
        img: res.data.imgPath,
        title: options.title,
        timeOut: (time - (new Date().getTime())) > 0 ? true : false,
        joinInfo: res.data.joinInfo
      })
      this.retJion(res.data.joinInfo)
      db.collection('collect').where({
        _openid: global.openid,
        activityId: activityId
      }).get().then(res => {
        this.setData({
          collect: res.data.length ? true : false
        })
        wx.hideLoading()
      }).catch(res => {
        func.hideLoading('加载失败')
      })
    }).catch(res => {
      func.hideLoading('加载失败')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})